//
//  RGEmailViewController.m
//  Gunz
//
//  Created by Joggy on 16/1/21.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "RGEmailViewController.h"

@interface RGEmailViewController ()

@end

@implementation RGEmailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
    UITapGestureRecognizer *endEditTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEditTap)];
    [self.view addGestureRecognizer:endEditTap];
}

- (void)prepareForNav {
    [super prepareForNav];
    self.title = @"账号注册";
    //返回按键
    UIImage *leftImage = [UIImage imageNamed:@"all_back"];
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, (leftImage.size.width/leftImage.size.height)*22, 22)];
    [leftButton setImage:leftImage forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [self navigationItem].leftBarButtonItem = leftBarItem;
}

- (void)prepareForLayout {
    [super prepareForLayout];
    //滚动视图
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight - 64)];
    scroll.contentSize = CGSizeMake(screenWidth, scroll.frame.size.height + 1);
    scroll.showsHorizontalScrollIndicator = false;
    scroll.showsVerticalScrollIndicator = false;
    scroll.delegate = self;
    //号码框
    UIView *emailView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 66, RGETextHeihgt)];
    _emailLabel = [[UILabel alloc] init];
    NSRange ran = {1, 2};
    NSMutableAttributedString *emailTitle = [[NSMutableAttributedString alloc] initWithString:@"邮尼玛箱 : "];
    [emailTitle addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:ran];
    _emailLabel.attributedText = emailTitle;
    _emailLabel.font = [UIFont systemFontOfSize:font15];
    _emailLabel.alpha = 0.7;
    [_emailLabel sizeToFit];
    _emailLabel.layer.position = CGPointMake(_emailLabel.frame.size.width/2, emailView.frame.size.height/2);
    [emailView addSubview:_emailLabel];
    _emailText = [[UITextField alloc] initWithFrame:CGRectMake(_emailLabel.frame.size.width, 0, RGETextWidth, RGETextHeihgt)];
    _emailText.placeholder = @"请输入邮箱地址";
    _emailText.backgroundColor = [UIColor whiteColor];
    _emailText.layer.cornerRadius = 6;
    _emailText.font = [UIFont systemFontOfSize:font14];
    _emailText.layer.borderColor = [UIColor colorWithRed:0.83 green:0.83 blue:0.83 alpha:1].CGColor;
    _emailText.layer.borderWidth = 1;
    _emailText.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, RGEInsert/4, RGETextHeihgt)];
    _emailText.leftViewMode = UITextFieldViewModeAlways;
    [emailView addSubview:_emailText];
    emailView.frame = CGRectMake(0, 0, _emailLabel.frame.size.width + _emailText.frame.size.width, RGETextHeihgt);
    emailView.layer.position = CGPointMake(screenWidth/2, emailView.frame.size.height/2 + RGEInsert);
    [scroll addSubview:emailView];
    //条款
    UIView *proView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 77, 77)];
    _selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectButton.frame = CGRectMake(0, 0, 16*proportion, 16*proportion);
    _selectButton.backgroundColor = [UIColor whiteColor];
    _selectButton.layer.borderWidth = 1;
    _selectButton.layer.borderColor = BGGREEN.CGColor;
    _selectButton.layer.cornerRadius = 4;
    [_selectButton setImage:[UIImage imageNamed:@"check"] forState:UIControlStateSelected];
    [_selectButton addTarget:self action:@selector(selectPro:) forControlEvents:UIControlEventTouchUpInside];
    [proView addSubview:_selectButton];
    //文字
    _proLabel = [[TYAttributedLabel alloc] initWithFrame:CGRectMake(_selectButton.frame.size.width + RGEInsert/5, 0, 10*font12, font12)];
    [_proLabel appendText:@"我同意"];
    UIButton *proButton = [UIButton buttonWithType:UIButtonTypeSystem];
    NSMutableAttributedString *proTitle = [[NSMutableAttributedString alloc] initWithString:@"群智社所有条款"];
    NSRange proRange = {0, proTitle.length};
    [proTitle addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:proRange];
    [proTitle addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:proRange];
    [proTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font11] range:proRange];
    [proButton setAttributedTitle:proTitle forState:UIControlStateNormal];
    [proButton addTarget:self action:@selector(provision) forControlEvents:UIControlEventTouchUpInside];
    proButton.frame = CGRectMake(0, 0, 7*font12, font12);
    [_proLabel appendView:proButton];
    _proLabel.font = [UIFont systemFontOfSize:font11];
    _proLabel.numberOfLines = 1;
    _proLabel.backgroundColor = [UIColor clearColor];
    [_proLabel sizeToFit];
    [proView addSubview:_proLabel];
    proView.frame = CGRectMake(0, 0, _selectButton.frame.size.width + _proLabel.frame.size.width + RGEInsert/5, _selectButton.frame.size.height);
    proView.layer.position = CGPointMake(emailView.frame.origin.x + emailView.frame.size.width - proView.frame.size.width + proView.frame.size.width/2 + 3, emailView.frame.origin.y + emailView.frame.size.height + RGEInsert);
    [scroll addSubview:proView];
    [self.view addSubview:scroll];
    //下一步按键
    _nextStepBarButton = [[BarButtonView alloc] initWithFrame:CGRectMake(0, screenHeight - 64 - BBViewHeight, screenWidth, BBViewHeight) name:@"下一步"];
    [_nextStepBarButton.but addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_nextStepBarButton];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)back {
    [self dismissViewControllerAnimated:true completion:nil];
}

- (void)selectPro:(id)sender {
    UIButton *but = (UIButton*)sender;
    but.selected = !but.selected;
    NSLog(@"选择");
    NSLog(@"%d", but.selected);
}

- (void)provision {
    NSLog(@"条款");
}

- (void)nextStep {
    NSLog(@"下一步");
}

- (void)endEditTap {
    [self.view endEditing:true];
}

-(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView {
    [self endEditTap];
}

@end
